package com.efast.dmom.mgmt.service.mes.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mgmt.bean.mes.ProductExcuteDto;
import com.efast.dmom.mgmt.dao.mes.TwBasLotConsumpationDao;
import com.efast.dmom.mgmt.entity.mes.TeMesOutbound;
import com.efast.dmom.mgmt.entity.mes.TrBasProductCfgBi;
import com.efast.dmom.mgmt.entity.mes.TwBasLot;
import com.efast.dmom.mgmt.entity.mes.TwBasLotConsumpation;
import com.efast.dmom.mgmt.service.mes.TwBasLotConsumpationService;
import com.efast.dmom.mgmt.utils.Assist;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TwBasLotConsumpationServiceImpl implements TwBasLotConsumpationService{
    @Autowired
    private TwBasLotConsumpationDao twBasLotConsumpationDao;
    @Override
    public long getTwBasLotConsumpationRowCount(Assist assist){
        return twBasLotConsumpationDao.getTwBasLotConsumpationRowCount(assist);
    }
    @Override
    public List<TwBasLotConsumpation> selectTwBasLotConsumpation(Assist assist){
        return twBasLotConsumpationDao.selectTwBasLotConsumpation(assist);
    }
    @Override
    public TwBasLotConsumpation selectTwBasLotConsumpationByObj(TwBasLotConsumpation obj){
        return twBasLotConsumpationDao.selectTwBasLotConsumpationByObj(obj);
    }
    @Override
    public TwBasLotConsumpation selectTwBasLotConsumpationById(Integer id){
        return twBasLotConsumpationDao.selectTwBasLotConsumpationById(id);
    }
    @Override
    public int insertTwBasLotConsumpation(TwBasLotConsumpation value){
        return twBasLotConsumpationDao.insertTwBasLotConsumpation(value);
    }
    @Override
    public int insertNonEmptyTwBasLotConsumpation(TwBasLotConsumpation value){
        return twBasLotConsumpationDao.insertNonEmptyTwBasLotConsumpation(value);
    }
    @Override
    public int insertTwBasLotConsumpationByBatch(List<TwBasLotConsumpation> value){
        return twBasLotConsumpationDao.insertTwBasLotConsumpationByBatch(value);
    }
    @Override
    public int deleteTwBasLotConsumpationById(Integer id){
        return twBasLotConsumpationDao.deleteTwBasLotConsumpationById(id);
    }
    @Override
    public int deleteTwBasLotConsumpation(Assist assist){
        return twBasLotConsumpationDao.deleteTwBasLotConsumpation(assist);
    }
    @Override
    public int updateTwBasLotConsumpationById(TwBasLotConsumpation enti){
        return twBasLotConsumpationDao.updateTwBasLotConsumpationById(enti);
    }
    @Override
    public int updateTwBasLotConsumpation(TwBasLotConsumpation value, Assist assist){
        return twBasLotConsumpationDao.updateTwBasLotConsumpation(value,assist);
    }
    @Override
    public int updateNonEmptyTwBasLotConsumpationById(TwBasLotConsumpation enti){
        return twBasLotConsumpationDao.updateNonEmptyTwBasLotConsumpationById(enti);
    }
    @Override
    public int updateNonEmptyTwBasLotConsumpation(TwBasLotConsumpation value, Assist assist){
        return twBasLotConsumpationDao.updateNonEmptyTwBasLotConsumpation(value,assist);
    }

    public TwBasLotConsumpationDao getTwBasLotConsumpationDao() {
        return this.twBasLotConsumpationDao;
    }

    public void setTwBasLotConsumpationDao(TwBasLotConsumpationDao twBasLotConsumpationDao) {
        this.twBasLotConsumpationDao = twBasLotConsumpationDao;
    }
    /**
     * 保存lot物料消耗记录
     */
	@Override
	public int saveLotConsumpations(ProductExcuteDto dto) {
		String currentUser = SpringWebUtils.getRequestUser();
		String companyCode = SpringWebUtils.getRequestCompany();
		TwBasLot lot = dto.getRowData();
		List<TwBasLotConsumpation> consumpationsList = new ArrayList<TwBasLotConsumpation>();
		for(TrBasProductCfgBi bi:dto.getConsumpation()) {
			TwBasLotConsumpation comsumpation = new TwBasLotConsumpation();
			comsumpation.setStationName(lot.getStationName());
			comsumpation.setComments(bi.getComments());
			comsumpation.setStationId(lot.getStationId());
			comsumpation.setLotName(lot.getLotName());
			comsumpation.setProductionLine(lot.getProductionLine());
			comsumpation.setRoute(lot.getRoute());
			comsumpation.setLotId(lot.getLotId());
			comsumpation.setConsumedPartSn(bi.getConsumedPartSn());
			comsumpation.setConsumedPartNoVersion(bi.getConsumedPartNoVersion());
			comsumpation.setOperationId(lot.getOperationId());
			comsumpation.setOperation(lot.getOperation());
			comsumpation.setConsumedQty(Double.valueOf(bi.getBomItemQty()));
			comsumpation.setProductCfgBiId(bi.getProductCfgItemId());
			comsumpation.setCreateUser(currentUser);
			comsumpation.setCreateTime(new Date());
			comsumpation.setMarkStatus(bi.getMarkStatus());
			comsumpation.setCompanyCode(companyCode);
			comsumpation.setOptCounter(bi.getOptCounter());
			comsumpation.setBomItemName(bi.getBomItemName());
			consumpationsList.add(comsumpation);
		}
		return twBasLotConsumpationDao.insertTwBasLotConsumpationByBatch(consumpationsList);
	}
	
	/**
	 * 批量更新lot物料消耗记录中的物料序号
	 */
	@Override
	public int updateLotConsumpationsPartSnByBatch(List<TwBasLotConsumpation> consumpationsList, ProductExcuteDto dto) {
		for(TrBasProductCfgBi bi:dto.getConsumpation()) {
			if(consumpationsList!=null && consumpationsList.size()>0) {
				for(int i=0;i<consumpationsList.size();i++) {
					if(consumpationsList.get(i).getConsumedPartNoVersion().equals(bi.getConsumedPartNoVersion())) {
						consumpationsList.get(i).setConsumedPartSn(bi.getConsumedPartSn());
						consumpationsList.get(i).setComments(bi.getComments());
					}
				}
			}
		}
		return twBasLotConsumpationDao.updateLotConsumpationsPartSnByBatch(consumpationsList);
	}
	@Override
	public List<TeMesOutbound> getConsumpationInfo(Integer lotId, Integer operationId) {
		return twBasLotConsumpationDao.getConsumpationInfo(lotId,operationId);
	}
}